<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){

    exit();

      // for ($i=980001; $i <=1050000 ; $i++) { 
      // $password['pa_number_before']=  md5(sha1(md5(sha1(sha1(sha1(md5(md5(md5(sha1(sha1(md5(md5($i)))))))))))));
      // $password['pa_number_after'] =$i;
      // M('password')->add($password);
      // }
      // $where['pa_number_before']='6e44b0cac9a69fcebdbc8aaf59061a33';
      // $info=M('password')->where($where)->find();
       $id=I('idinfo');
       $idinfo=strrev($id);
       if(empty($id)&&!empty($_SESSION['id_info'])){
       $substr= $_SESSION['id_info'];
       }elseif(!empty($id)){
       $substr= substr($idinfo, 1,32);
       }
       $w_petinfo['id_info']=$substr;
       $petinfo= M('petinfo')->where($w_petinfo)->find();
       if(!$petinfo){
       $w_password['pa_number_before']=$substr;
       $password_info=  M('password')->where($w_password)->find();
         if(!$password_info){
            echo '请输入正规的秘钥';
            exit();
         }else{
          $_SESSION['id_info']=$substr;
         }
       }else{
          $_SESSION['id_info']=$substr;
       }


       $w_petinfo['id_info']=$_SESSION['id_info'];
       $petinfo=M('petinfo')->alias('a')->join('LEFT JOIN lose_info as b ON a.tel=b.ls_tel ')->where($w_petinfo)->find();
       $show_status=empty($petinfo['ls_status'])?0:$petinfo['ls_status'];
       if($petinfo){
         $this->assign('show_content',$petinfo['ls_content']);
         $this->assign('show_status',$show_status);
         $this->assign('petname',$petinfo['petname']);
         $this->assign('tel',$petinfo['tel']);
         $this->assign('shop_tel',$petinfo['shop_tel']);
         $this->assign('password',$petinfo['password']);
         $this->display();
       }else{
         $this->assign('miyao',$this->miyao_set($substr));
         $this->display('index_one');  //初次进来
       }
       
    }
      //秘钥
     public function miyao_set($substr){
      $where['pa_number_before']=$substr;
      $password= M('password')->where($where)->getField('pa_number_after');
      $num=$password;
      $end=substr(sha1($num.'tff'),0,2);
      $numinfo=substr(md5(md5($num)),0,5).$num.$end;
      return $numinfo;
     } 

     public  function generate_password( $length ) { 
            // 密码字符集，可任意添加你需要的字符 
            $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 
            $password = ''; 
            for ( $i = 0; $i < $length; $i++ ) 
            { 
            // 这里提供两种字符获取方式 
            // 第一种是使用 substr 截取$chars中的任意一位字符； 
            // 第二种是取字符数组 $chars 的任意元素 
            // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1); 
            $password .= $chars[ mt_rand(0, strlen($chars) - 1) ]; 
            } 
            return $password; 
            }

    public function in_gongnengadd(){
     if(IS_POST){
       $save=I('save');
       $_POST['id_info']=$_SESSION['id_info'];
       $_POST['addtime']=time();
       $_POST['password']=md5(123456);
       if($save==1){
          $w_petinfo['id_info']=$_SESSION['id_info'];
          $a_petinfo_set=M('petinfo')->where($w_petinfo)->save($_POST);
       }else{
           $tel=I('shop_tel');
           $tel_password=M('tel_password')->where("tel_num=".$tel)->find();
           if(empty($tel_password)){
            $data['status']=103;
            $this->ajaxReturn($data);
           }
          $a_petinfo_set=M('petinfo')->add($_POST);
          $admin['admin_username']=$_POST['shop_tel'];
          $admin['admin_pid']=$a_petinfo_set;
          $a_admin=M('admin')->add($admin);

          if(!M('admin_user')->where("admin_u_tel=".$_POST['shop_tel'])->find()){
             $admin_user['admin_u_tel']=$_POST['shop_tel'];
             $admin_user['admin_u_password']=md5(123456);
             $admin_user['admin_u_add_time']=time();
             M('admin_user')->add($admin_user);
          }
       }
       if($a_petinfo_set||$a_admin){
        $data['info']=$_POST['id_info'];
        $data['status']=100;
        $this->ajaxReturn($data);
       }else{
        $data['status']=101;
        $this->ajaxReturn($data);
       }
     }else{
       $this->display();
     }
    }

public function in_gongnengsave(){
        $w_petinfo['id_info']=$_SESSION['id_info'];
        $petinfo=M('petinfo')->where($w_petinfo)->find();
        $petclassinfo= M('pet_class')->where("p_t_status=".$petinfo['pettype'])->select();
        $this->assign('info',$petinfo);
        $this->assign('petclassinfo',$petclassinfo);
        $this->display();
}


    public function petInfo(){
        $w_petinfo['id_info']=$_SESSION['id_info'];
        $petinfo=M('petinfo')->where($w_petinfo)->find();
        $petinfo['year']=$this->birthday($petinfo);
        $petinfo['petclass']=M('pet_class')->where("p_t_id=".$petinfo['petclass'])->getField('p_t_name');
        $w_write_info['w_idinfo']=$_SESSION['id_info'];
        $w_write_info['w_type']=1;
        $write_info=M('write_info')->order('w_add_time desc')->where($w_write_info)->select();
        foreach ($write_info as $key => $value) {
          $pic=explode("|", $write_info[$key]['w_pic']);
          array_pop($pic);
          $write_info[$key]['w_pic']=$pic;
        }
        $this->assign('write_info',$write_info);
        $this->assign('petinfo',$petinfo);
        $this->display();
    }

  public  function birthday($petinfo){
        $year=$petinfo['ye']*3600*24*365;
        $mo=$petinfo['mo']*3600*24*30;
        $da=$petinfo['da']*3600*24;
        $timeall=$year+$mo+$da+(time()-$petinfo['addtime']);
        $birthday= date("Y-m-d",time()-$timeall);
  $list = explode("-",$birthday);
  $year_diff = date("Y") - $list[0];
  $month_diff = date("m") - $list[1];

  if ($month_diff < 0){
       $year_diff--;
   $month_diff=12+date("m")-$list[1]; 
  }
   if($year_diff>0){
    return $year_diff.'年'.$month_diff.'个月';
   }else{
    return $month_diff.'个月';
   }
}
//使用功能
public function use_gongneng(){
  $w_id=I('w_id');
  if(!empty($w_id)){
  $write_info= M('write_info')->where("w_id=".$w_id)->find();
  $pic=explode("|", $write_info['w_pic']);
  array_pop($pic);
  $this->assign('write_info',$write_info);
  $this->assign('w_id',$write_info['w_id']);
  $this->assign('w_pic',$pic);
  }
  $write_info['w_wtime']=date('Y-m-d');
  $this->assign('write_info',$write_info);
  $this->display();
}
//添加宠物信息
public function addpetinfo(){
  $save=I('save');
  $w_write_info['w_wtime']=I('write_time');
  $w_write_info['w_content']=I('textarea_content');
  $w_write_info['w_add_time']=time();
  $w_write_info['w_idinfo']=$_SESSION['id_info'];
  if($save==1){
    $w_write_info_s['w_id']=I('w_id');
    $add_write_info=M('write_info')->where($w_write_info_s)->save($w_write_info); 
  }else{
    $img=I('imgs');
    array_pop($img);
    if(!empty($img)){
            $address='Uploads/pet/'.date('Y-m-d',time()).'/';
            $w_write_info['w_pic']=$this->sendpic($img,$address);
         }
    $w_write_info['w_type']=$_POST['w_type'];     
    $add_write_info=M('write_info')->add($w_write_info); 
  }
  if($add_write_info){
     $this->ajaxReturn(100);
  }else{
     $this->ajaxReturn(101);
  }                                                                                    
}
//
public function delete_write(){
   $w_id=I('w_id');
   $info= M('write_info')->delete($w_id);
   if($info){
     $this->ajaxReturn(100);
   }else{
     $this->ajaxReturn(101);
   }
}
 public function er_tel_info(){
 $tel=I('tel');
 $info=M('info')->where("ls_tel=".$tel)->find();
 $info['ipdb_info']=M('ip')->order('ip_addtime')->where('ip_tel='.$tel)->field('ip_address,ip_addtime')->select();
 if($info){
   $this->ajaxReturn($info);
 }
 }

  public function er_tel_info_submit(){
     $content=I('content');
     $status=I('status');
     $subtel=strrev(I('tel'));
     $where_lo_one['pa_number_before']=substr($subtel,0,32);
     $where_lo_two['pa_number_before']=substr($subtel,32,32);
     $telone=M('password')->where($where_lo_one)->getField('pa_number_after');
     $teltwo=M('password')->where($where_lo_two)->getField('pa_number_after');
     $tel=$telone.$teltwo;
     $info=M('info')->where("ls_tel=".$tel)->find();
     if($info){
       $losw_info['ls_tel']=$tel;
       $s_l_info['ls_status']=$status;
       $s_l_info['ls_content']=$content;
       $info=M('info')->where($losw_info)->save($s_l_info); 
       if($info){
        $this->ajaxReturn('ok');
      }else{
        $this->ajaxReturn('no');
      }
     }else{
      $a_l_info['ls_content']=$content;
      $a_l_info['ls_tel']=$tel;
      $info=M('info')->add($a_l_info); 
      if($info){
        $this->ajaxReturn('ok');
      }else{
        $this->ajaxReturn('no');
      }
     }
 }


  //多图上传
    public function sendpic($img,$address){
       for($i=0;$i<count($img);$i++){
           $newImgType= substr($img[$i],0,strrpos($img[$i],';'));  //图片的类型
           $base=substr($img[$i],strrpos($img[$i],';')+1);
           $a=explode(',',$base);
           $types=explode('/',$newImgType);
           $type=$types[1];
           $filename = rand(0,100000).time().".".$type; 
           $xmlstr=base64_decode($a[1]);
           makeDir($address);
           $file = fopen($address.$filename,"w");//打开文件准备写入  
           fwrite($file,$xmlstr);//写入
           fclose($file);//关闭
           $fileaddress[]='/'.$address.$filename;
       } 
       $info=implode('|',$fileaddress);
       return $info.'|';
    }
 //宠物相册
    public function photo(){
        $w_write_info['w_idinfo']=$_SESSION['id_info'];
        $w_write_info['w_type']=2;
        $write_info=M('write_info')->order('w_add_time desc')->where($w_write_info)->select();
        foreach ($write_info as $key => $value) {
          $pic=explode("|", $write_info[$key]['w_pic']);
          array_pop($pic);
          $write_info[$key]['w_pic']=$pic;
        }
        $this->assign('write_info',$write_info);
        $this->assign('petinfo',$petinfo);
        $this->display();
    }
 //宠物相册 添加
 public function photo_add(){
  $write_info['w_wtime']=date('Y-m-d');
  $this->assign('write_info',$write_info);
  $this->display();
 } 
//删除 添加后的info 还有添加 手机号码
  public function deleteinfo(){
    if(IS_POST){
        $num=I('num');
        $tel=I('tel');
        if(!empty($num)){
           if($num==3315){
              $de_where['id_info']='512338c737bccc02cbb6eea5816eae84';
              $info=M('petinfo')->where($de_where)->delete();
              if($info){
                $this->ajaxReturn(100); 
              }else{
                $this->ajaxReturn(101);
              }
           }
        }
        if(!empty($tel)){
           $add['tel_num']=$tel;
           $add['tel_over_num']=100;
           $w_find['tel_num']=$tel;
           $f_info= M('tel_password')->where($w_find)->find();
           if($f_info){
             $this->ajaxReturn(102);
           }
           $info= M('tel_password')->add($add);
           if($info){
            $this->ajaxReturn(100);
           }else{
            $this->ajaxReturn(101);
           }
        }
    }else{
       $this->display();
    }
 } 

 public function ipadd(){
  $id_info['id_info']=$_SESSION['id_info'];
  $tel=M('petinfo')->where($id_info)->getField('tel');
  $ipadd['ip_tel']=$tel;
  $ipadd['ip_lnt']=I('lnt');
  $ipadd['ip_lat']=I('lat');
  $ipadd['ip_idinfo']=$_SESSION['id_info'];
  $ipadd['ip_addtime']=time();
  $ipadd['ip_address']=I('address');
  M('ip')->add($ipadd);
 } 
}